package com.imooc.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * HTTP://youngpeng.natapp1.cc/sell/weixin/auth
 * https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
 *
 * https://open.weixin.qq.com/connect/oauth2/authorize?appid= wx5e00123f322f6202&redirect_uri= HTTP://youngpeng.natapp1.cc/sell/weixin/auth &response_type=code&scope= snsapi_base &state=STATE#wechat_redirect
 */
@RestController
@Slf4j
@RequestMapping("/weixin")
public class WeixinController {
    @GetMapping("/auth")
    public void auth(@RequestParam("code") String code){
        //@RequestParam("code") String code
        log.info("进入auth验证。。。。。。");
        log.info("这是我的code={}",code);

        /**获取到参数之后。，用code 换取 access_token -- 服务号不一样
         * 这里可以用自动的取代替。
         * 【  https://github.com/Wechat-Group/weixin-java-tools  】
         */
        String url = "https://api.wixin.qq.com/sns/auth2/access_token?appid=APPID&secret=APPSCREET&code=" + "CODE" + "&grant_type=authorization_code";
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject(url, String.class);
        log.info("response-{}", response);
    }
}
